package com.bawei.module_park.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bawei.lib_image.GlideApp
import com.bawei.module_park.R
import com.bawei.module_park.databinding.ItemAdBannerBinding
import com.bawei.module_park.databinding.ItemGridItemBinding
import com.bawei.module_park.databinding.ItemParkNoticeBinding
import com.bawei.module_park.databinding.ItemRowTitleBinding
import com.bawei.module_park.databinding.ItemSearchBinding
import com.bawei.module_park.model.ContentItem

/**
 *作者：lxy
 *功能:
 *时间：2025/6/12
 */
class ParkHomeMultiAdapter(
    private val items: List<ContentItem>,
    private val onItemClick: ((ContentItem.FunctionItem) -> Unit)? = null,
) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    companion object {
        private const val TYPE_SEARCH = 0
        private const val TYPE_AD_ITEM = 1
        private const val TYPE_FUNCTION = 2
        private const val TYPE_ROW_TITLE = 3
        private const val TYPE_NOTICE =4
    }

    override fun getItemViewType(position: Int): Int =
        when (items[position]) {
            is ContentItem.SearchBarItem -> TYPE_SEARCH
            is ContentItem.AdItem -> TYPE_AD_ITEM
            is ContentItem.FunctionItem -> TYPE_FUNCTION
            is ContentItem.RowTitle -> TYPE_ROW_TITLE
            is ContentItem.NoticeItem -> TYPE_NOTICE
        }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        when (viewType) {
            TYPE_SEARCH -> {
                val binding: ItemSearchBinding = DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    R.layout.item_search,
                    parent,
                    false
                )
                return SearchViewHolder(binding.root)
            }

            TYPE_AD_ITEM -> {
                val binding: ItemAdBannerBinding = DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    R.layout.item_ad_banner,
                    parent,
                    false
                )
                return AdViewHolder(binding.root)
            }

            TYPE_FUNCTION -> {
                val binding: ItemGridItemBinding = DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    R.layout.item_grid_item,
                    parent,
                    false
                )
                return FunctionViewHolder(binding.root)
            }
            TYPE_ROW_TITLE -> {
                val binding: ItemRowTitleBinding = DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    R.layout.item_row_title,
                    parent,
                    false
                )
                return RowTitleViewHolder(binding.root)
            }
            TYPE_NOTICE -> {
                val binding: ItemParkNoticeBinding = DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    R.layout.item_park_notice,
                    parent,
                    false
                )
                return NoticeViewHolder(binding.root)
            }

            else -> throw IllegalArgumentException("无效的视图类型")
        }
    }

    override fun getItemCount(): Int {
        return items.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (items[position]) {
            is ContentItem.SearchBarItem -> {
                val binding =
                    DataBindingUtil.getBinding<ItemSearchBinding>(holder.itemView)
            }

            is ContentItem.AdItem -> {
                val binding =
                    DataBindingUtil.getBinding<ItemAdBannerBinding>(holder.itemView)

            }

            is ContentItem.FunctionItem -> {
//                val item = items[position] as ContentItem.FunctionItem
//                val binding =
//                    DataBindingUtil.getBinding<ItemGridItemBinding>(holder.itemView)
//                (holder as FunctionViewHolder).init(item)
//                (holder as FunctionViewHolder).clickItem()
                val item = items[position] as ContentItem.FunctionItem
                (holder as FunctionViewHolder).bind(item, onItemClick)
            }


            is ContentItem.RowTitle -> {
                val item = items[position] as ContentItem.RowTitle
                val binding =
                    DataBindingUtil.getBinding<ItemRowTitleBinding>(holder.itemView)
                if (binding != null) {
                    convertTitle(binding, item)
                }
            }
            is ContentItem.NoticeItem -> {
                val item = items[position] as ContentItem.NoticeItem
                (holder as NoticeViewHolder).bind(item)
            }
        }
    }


    private fun convertTitle(binding: ItemRowTitleBinding, item: ContentItem.RowTitle) {
        binding.item = item
    }

    private fun convertFunctionItem(binding: ItemGridItemBinding, item: ContentItem) {
        binding.item = item

        // 查看商品详情
        binding.root.setOnClickListener {

        }
    }


    inner class FunctionViewHolder(
        itemView: View,
        private val onItemClick: (() -> Unit)? = null
    ) : RecyclerView.ViewHolder(itemView) {
        private val mBinding: ItemGridItemBinding by lazy {
            DataBindingUtil.getBinding(this.itemView)!!
        }

        // 单参数bind方法，依赖外部设置的点击回调
        fun bind(item: ContentItem.FunctionItem, onItemClick: ((ContentItem.FunctionItem) -> Unit)?) {
            // 使用原始init方法的功能
            GlideApp.with(itemView.context).load(item.icon).into(mBinding?.gridIv!!)
            mBinding.gridTitle.text = item.iconType

            // 3. 直接在bind方法中设置点击事件
            mBinding.lGridItem.setOnClickListener {
                onItemClick?.invoke(item)
            }
        }

    }
}

/**
 * 搜索viewHolder
 */
class SearchViewHolder(itemView: View, private val onSearchClick: (() -> Unit)? = null) :
    RecyclerView.ViewHolder(itemView) {
    private val mBinding: ItemSearchBinding by lazy {
        DataBindingUtil.getBinding(this.itemView)!!
    }


    fun clickSearch() {
        mBinding.ivSearch.setOnClickListener {
            onSearchClick?.invoke()
        }
    }


}



class AdViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){}

class RowTitleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    private val mBinding: ItemRowTitleBinding by lazy {
        DataBindingUtil.getBinding(this.itemView)!!
    }

    fun convertTitle(binding: ItemRowTitleBinding, item: ContentItem.RowTitle) {
        binding.item = item
    }
}
class NoticeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    private val mBinding: ItemParkNoticeBinding by lazy {
        DataBindingUtil.getBinding(this.itemView)!!
    }

    fun bind(item: ContentItem.NoticeItem) {
        itemView.apply {
            mBinding.noticeTitle.text = item.title
            mBinding.noticeContent.text = item.content
            mBinding.noticeTime.text = item.date
        }
    }
}